package com.itheima.health.controller;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/1/17 10:27
 * @Version 1.0
 */

public class OrderSettingController {
    public static void main(String[] args) throws IOException {
        //1 打开一个已经创建好的工作薄并读取
        //XSSFWorkbook workbook = new XSSFWorkbook("E:\\software2\\feiq\\download\\项目一\\2021-1-4传智健康05\\03-其他资料\\poi_test.xlsx");
        //遍历sheet页的第一种方式
//        int numberOfSheets = workbook.getNumberOfSheets();
//        for (int i = 0; i < numberOfSheets; i++) {
//           Sheet sheet = workbook.getSheetAt(i);
//            System.out.println("======"+sheet.getSheetName());
//            //遍历row行
//            int lastRowNum = sheet.getLastRowNum();
//            for (int j = 0; j <= lastRowNum; j++) {
//                Row row = sheet.getRow(j);
//                //遍历cell单元格
//                short lastCellNum = row.getLastCellNum();
//                for (int k = 0; k < lastCellNum; k++){
//                    Cell cell = row.getCell(k);
//                    System.out.print(cell.getStringCellValue()+"\t");
//                }
//                System.out.println("");
//            }
//        }
        //遍历sheet页的第二种方式
//        for (Sheet sheet : workbook) {
//            System.out.println("======"+sheet.getSheetName());
//            //遍历行
//            for (Row row : sheet) {
//                //遍历单元格
//                for (Cell cell : row) {
//                    System.out.print(cell.getStringCellValue()+"\t");
//                }
//                System.out.println("");
//            }
//        }
//        workbook.close();
        //1 创建一个工作薄并读取

        //在内存中创建一个Excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表，指定工作表名称
        XSSFSheet sheet = workbook.createSheet("传智播客");

        //创建行，0表示第一行
        XSSFRow row = sheet.createRow(0);
        //创建单元格，0表示第一个单元格
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("名称");
        row.createCell(2).setCellValue("年龄");

        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("1");
        row1.createCell(1).setCellValue("小明");
        row1.createCell(2).setCellValue("10");

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("2");
        row2.createCell(1).setCellValue("小王");
        row2.createCell(2).setCellValue("20");

        //通过输出流将workbook对象下载到磁盘
        FileOutputStream out = new FileOutputStream("E:\\itcast.xlsx");
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();

    }
}
